import request from '../../utils/request'
import { setTokenInfo ,removeTokenInfo} from '../../utils/storage'

export const sendCode = (mobile) => {
  return async () => {
    // 发送请求
    const res = await request({
      url: `/sms/codes/${mobile}`,
      method: 'get',
    })
    console.log(res)
  }
}
export const saveToken = (payload) => {
  return {
    type: 'login/token',
    payload,
  }
}
/**
 * 登录功能
 * @param {*} data
 * @returns
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await request({
      method: 'post',
      url: '/authorizations',
      data,
    })
    // console.log('res', res.data.data)
    // 保存token到redux中
    dispatch(saveToken(res.data.data)) //注意这里要两个data
    // 保存到本地
    setTokenInfo(res.data.data)
  }
}

// logout
/**
 * 退出
 * @returns
 */
export const logout2 = () => {
  return (dispatch) => {
    // 移除本地token
    removeTokenInfo()
    // 移除redux中的token
    dispatch({
      type: 'login/logout',
    })
  }
}
